<template>
  <div v-if="mode==9 || mode == 10">
    <el-row v-for="(row,index) in rows" :key="index" :gutter="4" style="margin-bottom: 5px;">
      <el-col :span="22">
        <el-input v-model="row.Val" placeholder="匹配值" :max-length="20" clearable @change="save" />
      </el-col>
      <el-col :span="2">
        <el-button type="danger" size="mini" icon="el-icon-delete" circle @click="drop(index)" />
      </el-col>
    </el-row>
    <el-button type="primary" icon="el-icon-plus" @click="add">添加值</el-button>
  </div>
  <div v-else-if="link.includes(mode)">
    <el-input v-model="val.Val" placeholder="匹配值" :max-length="20" clearable @change="save" />
  </div>
  <div v-else-if="mode == 5">
    <el-row :gutter="4">
      <el-col :span="12">
        <filterValue v-model="begin" @change="save" />
      </el-col>
      <el-col :span="12">
        <filterValue v-model="end" @change="save" />
      </el-col>
    </el-row>
  </div>
  <div v-else>
    <filterValue v-model="val" @change="save" />
  </div>
</template>

<script>
import filterValue from '@/customForm/components/pageFilter/filterValue.vue'
export default {
  components: {
    filterValue
  },
  props: {
    mode: {
      type: Number,
      default: 0
    },
    value: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      rows: [],
      link: [6, 7, 8, 12],
      val: null,
      begin: null,
      end: null
    }
  },
  watch: {
    mode: {
      handler(val) {
        this.reset()
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    reset() {
      this.val = null
      this.begin = null
      this.end = null
      this.rows = []
      if (this.mode === 5) {
        this.begin = this.value && this.value.length > 0 ? this.value[0] : {
          ValueType: 0,
          Val: null
        }
        this.end = this.value && this.value.length > 1 ? this.value[1] : {
          ValueType: 0,
          Val: null
        }
      } else if (this.mode === 9 || this.mode === 10) {
        if (this.value && this.value.length > 0) {
          this.rows = this.value[0].Vals.map(c => {
            return {
              Val: c
            }
          })
        } else {
          this.rows = []
        }
      } else {
        this.val = this.value && this.value.length > 0 ? this.value[0] : {
          ValueType: 0,
          Val: null
        }
      }
    },
    add() {
      this.rows.push({
        Val: null
      })
      this.save()
    },
    drop(index) {
      this.rows.splice(index, 1)
      this.save()
    },
    save() {
      let t
      if (this.mode === 5) {
        t = [this.begin, this.end]
      } else if (this.mode === 9 || this.mode === 10) {
        const list = this.rows.map(c => c.Val)
        t = [{
          ValueType: 3,
          Vals: list
        }]
      } else {
        t = [this.val]
      }
      this.$emit('input', t)
      this.$emit('change', t)
    }
  }
}
</script>
